<?php

class PhotosController extends Controller
{
  public $layout='//layouts/main';

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index', 'create', 'update', 'destroy'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionIndex()
	{
    $user = Users::model()->find('id = :id', array(':id' => $_SESSION['id']));
    $photo = Photos::model()->with("user")->find('profile_picture = :profile_picture and user_id = :user_id', array(":profile_picture" => 1, "user_id" => $_SESSION['id']));
    $photos = Photos::model()->findAll('user_id = :user_id AND profile_picture = 0', array("user_id" => $_SESSION['id']));

    $member_id = $_SESSION['member_id'];
    $pageSize = 10;

    //News------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $news = News::model()->findAll($criteria);

    //Articles-------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $articles = Articles::model()->findAll($criteria);

    //Links-------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $links = Links::model()->findAll($criteria);

    //Journals-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $journals_pages = new CPagination(Journals::model() -> count());
    $journals_pages -> pageSize = $pageSize;
    $journals_pages -> applyLimit($criteria);

    $journals = Journals::model()->findAll($criteria);

    //Webinars-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $webinars_pages = new CPagination(Webinars::model() -> count());
    $webinars_pages -> pageSize = $pageSize;
    $webinars_pages -> applyLimit($criteria);

    $webinars = Webinars::model()->findAll($criteria);

    //Download URLs-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $downloadUrls_pages = new CPagination(DownloadUrls::model() -> count());
    $downloadUrls_pages -> pageSize = $pageSize;
    $downloadUrls_pages -> applyLimit($criteria);

    $downloadUrls = downloadUrls::model()->findAll($criteria);


    $model = new Photos;

		$this->render('index',array(
			'photos'=>$photos,
      'user'=>$user,
      'model' => $model,
      'profile_picture' => $photo,

      'news' => $news,
      'articles' => $articles,
      'links' => $links,
      'journals' => $journals,
      'journals_pages' => $journals_pages,
      'webinars' => $webinars,
      'webinars_pages' => $webinars_pages,
      'downloadUrls' => $downloadUrls,
      'downloadUrls_pages' => $downloadUrls_pages,
		));
	}

  public function actionCreate()
  {
    $model = new Photos('create');

    if(isset($_POST['Photos']))
    {
      $model->attributes = $_POST['Photos'];
      $model->content=CUploadedFile::getInstance($model,'upload');
      $model->user_id = $_SESSION['id'];

      $exist = Photos::model()->find('user_id = :user_id and profile_picture = 1', array(':user_id'=>$_SESSION['id']));

      $model->profile_picture = 1;

      if($model->validate('create'))
			{
        $model->save();
        $exist->profile_picture = 0;
        $exist->save();
				$model->content->saveAs(Yii::app()->basePath. '/../assets/users/'.$_SESSION['id'] .'/'. $model->content);

        Yii::app()->user->setFlash('success', 'Success');
        Yii::app()->user->setFlash('content', 'You have successfully upload new photo.');

				$this->redirect(array('index'));
			}
      Yii::app()->user->setFlash('error', 'Error');
      Yii::app()->user->setFlash('content', 'You can only upload JPG/JPEG/PNG/GIF format files!');
    }

    $this->redirect(array('index'));
  }

  public function actionUpdate()
  {
    $id = $_GET['id'];
    $photo = Photos::model()->find('id = :id', array(':id' => $id));
    $exist = Photos::model()->find('user_id = :user_id and profile_picture = 1', array(':user_id'=>$_SESSION['id']));

    $photo->profile_picture = 1;

    if($photo->save())
    {
      if(isset($exist))
      {
        $exist->profile_picture = 0;
        $exist->save();
      }

      Yii::app()->user->setFlash('success', 'Success');
      Yii::app()->user->setFlash('content', 'You have successfully set new profile picture.');

      $this->redirect(array('index'));
    }
  }

  public function actionDestroy()
  {
    $photo = Photos::model()->find('id = :id', array(':id' => $_GET['id']));
    unlink('assets/users/'.$_SESSION['id'].'/'.$photo->content);
    $photo->delete();
    $this->redirect(array('index'));
  }
}